package com.hy.service.impl;

import com.hy.dao.FollowDao;
import com.hy.service.FollowService;
import com.hy.utils.ResponseResult;
import com.hy.utils.ResultUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class FollowServiceImpl implements FollowService {
    private FollowDao followDao = new FollowDao();

    @Override
    public void followOrUnfollowUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //, Integer userId, Integer followId, String flowStatus

        String flowStatus = request.getParameter("flowStatus");
        if (flowStatus==null || flowStatus==""){
            System.out.println("flowStatus为空");
        }
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        if (userId==null || userId==0){
            System.out.println("userId为空");
        }
        Integer followId = Integer.valueOf(request.getParameter("followId"));

        ResponseResult<Boolean> rr = null ;
        if(flowStatus != null && flowStatus.equals("关注")){
            boolean exists = followDao.selectExists(userId, followId);
            boolean followUser = followDao.followUser(exists, userId, followId);
            rr = new ResponseResult<>(200,"关注成功",followUser,101);

        }else if(flowStatus != null && flowStatus.equals("已关注")){
            boolean unfollowUser = followDao.unfollowUser(userId, followId);
            rr = new ResponseResult<>(200,"取关成功",unfollowUser,101);
        }

        ResultUtils.result(rr,request,response);
    }
    //查询推荐关注
    public void selectRecommended(HttpServletRequest request, HttpServletResponse response) throws Exception{
        int count = followDao.selectCount(Integer.valueOf(request.getParameter("userId")));
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        if (count>=6){
            int anInt = new Random().nextInt(count-6);
            List<Map<String, Object>> mapList = followDao.selectRecommend(userId, anInt);
            ResultUtils.result(new ResponseResult<>(200,"推荐关注",mapList,101),request,response);
        }else {
            List<Map<String, Object>> mapList = followDao.selectRecommend(userId, 0);
            ResultUtils.result(new ResponseResult<>(200,"推荐关注",mapList,101),request,response);
        }
        return;
    }
}
